package org.example.exampreparationtreasure.controller;

import org.example.exampreparationtreasure.common.CommonResultStatus;
import org.example.exampreparationtreasure.mapper.UserMapper;
import org.example.exampreparationtreasure.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;

    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestParam String username,@RequestParam String password,@RequestParam String role){
        boolean result = userService.register(username,password,role);
        Map<String, Object> body = new HashMap<>();
        body.put("success", result);
        body.put("code", result ? CommonResultStatus.OK.getCode() : CommonResultStatus.FAIL.getCode());
        body.put("message", result ? CommonResultStatus.OK.getMessage() : CommonResultStatus.FAIL.getMessage());
        return ResponseEntity.ok(body);
    }

    @GetMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestParam String username, @RequestParam String password)
    {
        String token = userService.login(username,password);
        Map<String, Object> body = new HashMap<>();
        body.put("success", true);
        body.put("code", CommonResultStatus.OK.getCode());
        body.put("message", CommonResultStatus.OK.getMessage());
        body.put("token", token);
        return ResponseEntity.ok(body);
    }

}
